package cn.hefan.web.servlet.task;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.hefan.domain.ResultInfo;
import cn.hefan.domain.Task;
import cn.hefan.domain.User;
import cn.hefan.service.TaskService;

public class taskServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
				//解决中午乱码问题
				request.setCharacterEncoding("UTF-8");
				SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");//设置日期格式
		       String stime = df.format(new Date());
				
				
				Map<String, String[]> map = request.getParameterMap();
				//创建task对象
				Task task = new Task();
				try {
					BeanUtils.populate(task, map);
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				task.setTaskstatus("未完成");
				task.setStime(stime);
				ResultInfo info = null;
				if(task.getTaskname()!=null&&task.getTaskintd()!=null&&task.getDivide()!=null&&task.getEtime()!=null) {
					info = TaskService.addTask(task);
				}else {
					info = new ResultInfo(false);
				}
				
				//响应数据
				ObjectMapper mapper = new ObjectMapper();
				response.setContentType("application/json;charset=utf-8");
				mapper.writeValue(response.getOutputStream(),info);
				

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

}